//
//  MNDRechargeSMSViewController.m
//  minidai
//
//  Created by 容贤郁 on 2018/1/23.
//  Copyright © 2018年 迷你金服. All rights reserved.
//

#import "MNDRechargeSMSViewController.h"
#import "UIButton+JKCountDown.h"
#import "MNDRechargeResultViewController.h"

@interface MNDRechargeSMSViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *captchaTextField;
@property (weak, nonatomic) IBOutlet UIButton *captchaButton;

@property (nonatomic, assign) BOOL isSendCaptcha;

@end

@implementation MNDRechargeSMSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.phoneTextField.text = self.bankModel.jxBankaccount.userAmountMobile;
    
    __block UITextField *phoneTextField = self.phoneTextField;
    [self.phoneTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            phoneTextField.text = [x substringToIndex:11];
        }
    }];
    
    __block UITextField *codeTextField = self.captchaTextField;
    [self.captchaTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            codeTextField.text = [x substringToIndex:6];
        }
    }];
    
//    if (self.bankModel.jxBankaccount.userAmountMobile.length == 11) {
//        [self sendCaptchaAction:self.captchaButton];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sendCaptchaAction:(UIButton *)sender {
    if (self.phoneTextField.text.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入银行预留手机号"];
        return;
    }
    
    if (self.phoneTextField.text.length != 11) {
        [NetworkProgressHUD showHudMsg:@"请输入11位手机号码"];
        return;
    }
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"phone_num": self.phoneTextField.text, @"channel": @"app"}];
    
    [NetworkProgressHUD showHud];
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_RECHARGE_SEND_CAPTCHA parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        NSLog(@"response: %@", responseModel.data);
        weakself.isSendCaptcha = YES;
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            [sender jk_startTime:60 title:@"重新获取" waitTittle:@"秒后重发"];
            
        } else {
            
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
            } else {
                [NetworkProgressHUD showHudMsg:@"发送失败，请重试"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (IBAction)rechargeAction:(UIButton *)sender {
    
    if (self.phoneTextField.text.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入手机号码"];
        return;
    }
    
    if (self.phoneTextField.text.length != 11) {
        [NetworkProgressHUD showHudMsg:@"请输入11位手机号码"];
        return;
    }
    
    if (!self.isSendCaptcha) {
        [NetworkProgressHUD showHudMsg:@"请先获取验证码"];
        return;
    }
    
    if (self.captchaTextField.text.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入短信验证码"];
        return;
    }
    self.isSendCaptcha = NO;
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"recharge_money": self.rechargeMoney,
                                                          @"recharge_smscode": self.captchaTextField.text,
                                                          @"phone_num": self.phoneTextField.text,
                                                          @"channel": @"app"}];
    
    [NetworkProgressHUD showHud];
    WeakSelf(self);
    [APIManager sendRequestWithPath:API_RECHARGE_SUBMIT parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        NSLog(@"response: %@", responseModel);
        [NetworkProgressHUD hideHud];
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[MNDRechargeResultViewController description] bundle:nil];
        MNDRechargeResultViewController *resultVC = [storyboard instantiateViewControllerWithIdentifier:[MNDRechargeResultViewController description]];
        resultVC.rechargeResult = responseModel;
        
        if (weakself.isFromTender) {
            resultVC.isFromTender = weakself.isFromTender;
        }
        
        [weakself.navigationController pushViewController:resultVC animated:YES];
//        if (responseModel.status == ResponseSuccess) {
//
//        } else {
//            NSDictionary *dict = responseModel.msg;
//            if (dict && dict.count > 0) {
//                [NetworkProgressHUD showHudMsg:dict[@"error"]];
//            } else {
//                [NetworkProgressHUD showHudMsg:@"发送失败，请重试"];
//            }
//        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
